﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ReactiveCode.Util;

namespace ReactiveCode.Linq
{
    class EnumerableSubject<T> : IEnumerableSubject<T>
    {
        LinkedList<IEnumerableObserver<T>> _observers = new LinkedList<IEnumerableObserver<T>>();

        public IDisposable Subscribe(IEnumerableObserver<T> observer)
        {
            var node = _observers.AddLast(observer);
            return Disposable.Create(delegate
            {
                _observers.Remove(node);
            });
        }

        public void OnReset(IEnumerable<IObservable<T>> items)
        {
            foreach (var o in _observers)
                o.OnReset(items);
        }
        public void OnAdded(IObservable<T> item, int index)
        {
            foreach (var o in _observers)
                o.OnAdded(item, index);
        }
        public void OnRemoved(IObservable<T> item, int index)
        {
            foreach (var o in _observers)
                o.OnRemoved(item, index);
        }

        public void OnError(Exception error)
        {
            foreach (var o in _observers)
                o.OnError(error);
        }
        public void OnCompleted()
        {
            foreach (var o in _observers)
                o.OnCompleted();
        }
    }
}